package com.redijedi.tapestry.internal.tree;

import static com.redijedi.tapestry.internal.tree.TreeRendererCommand.RENDER_BODY;

import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.ValueEncoder;

import com.redijedi.tapestry.ITreeNodeModel;

/**
 * @author torr
 * 
 */
public class TreeRenderer {

	private final MarkupWriter _writer;

	@SuppressWarnings("all")
	private final ValueEncoder _encoder;

	public TreeRenderer(MarkupWriter writer) {
		_writer = writer;
		_encoder = null;
	}

	public void write(TreeRendererCommandNodeSet commandSet) {
		ITreeNodeModel<?> model = commandSet.getNode();
		TreeRendererCommand command = commandSet.getCommand();

		switch (command.getState()) {
		case OPEN:
			_writer.element(command.getElementName());
			break;
		case CLOSE:
			_writer.end();
			break;
		default:
			if (model.display() && command == RENDER_BODY) {
				_writer.write(model.getValue().toString());
			}
			break;
		}
	}
}
